package com.sbox;

import android.content.Context;
import android.view.View;
import android.view.ViewGroup;
import android.view.LayoutInflater;
import android.widget.BaseAdapter;
import android.widget.TextView;

import java.util.Calendar;
import java.util.Date;
import java.util.List;
import java.util.ArrayList;

import com.sbox.model.FilterPeriod;
import com.sbox.model.NoteItem;
import com.sbox.util.SBoxLog;
import com.sbox.util.Utils;
import com.sbox.R;

public class NoteAdapter extends BaseAdapter {
	private LayoutInflater mInflater = null;
	private List<NoteItem> mNotes = new ArrayList<NoteItem>();
	private List<NoteItem> mBackupNotes = new ArrayList<NoteItem>();
	private String mStrMonth = null;
	private String mStrDay = null;
	
	public NoteAdapter(Context c)
	{
		mInflater = (LayoutInflater)c.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
		
		mStrMonth = c.getText(R.string.str_month).toString();
		mStrDay = c.getText(R.string.str_day).toString();
	}
	
	public void add(NoteItem note)
	{
		if( note == null )
		{			
			return;
		}
		
		mNotes.add(note);
	}
	
	public void add(NoteItem[] notes)
	{
		if( notes == null )
			return;
		
		for(NoteItem note: notes)
		{					
			add(note);
		}
	}
	
	public void filter(String filter, int period)
	{		
		if( (filter == null || filter.length() == 0) && period == FilterPeriod.PERIOD_ALL )
		{
			restore();
			return;
		}
		
		if( mBackupNotes.size() == 0  )
		{
			//If this is the first filter, backup the objects.
			mBackupNotes.addAll(mNotes);
		}
		else
		{
			//not the first filter, restore the objects first.
			mNotes.clear();
			mNotes.addAll(mBackupNotes);
		}		
		
		long now = Calendar.getInstance().getTimeInMillis();
		ArrayList<NoteItem> tempList = new ArrayList<NoteItem>();
		
		if( period == FilterPeriod.PERIOD_ALL )
		{
			for( NoteItem note: mNotes )
			{
				if( !note.Title.contains(filter) )
				{				
					tempList.add(note);
				}
			}
		}
		else
		{
			if( filter != null && filter.length()> 0 )
			{
				for( NoteItem note: mNotes )
				{
					if( !note.Title.contains(filter) || !Utils.isNoteInPeriod(now, note.ID, period) )
					{				
						tempList.add(note);
					}
				}
			}
			else
			{				
				for( NoteItem note: mNotes )
				{					
					if( !Utils.isNoteInPeriod(now, note.ID, period) )
					{				
						tempList.add(note);
					}
				}
			}
		}
		
		for( NoteItem note : tempList) 
		{
			mNotes.remove(note);
		}
		
		tempList.clear();
	}
	
	public void restore()
	{
		if( mBackupNotes.size() != 0 )
		{
			mNotes.clear();
			mNotes.addAll(mBackupNotes);
			mBackupNotes.clear();
		}
	}
	
	@Override
	public int getCount() {
		// TODO Auto-generated method stub
		return mNotes.size();
	}

	@Override
	public Object getItem(int position) {
		// TODO Auto-generated method stub
		if( position < 0 || position >= mNotes.size() )
			return null;
		
		return mNotes.get(position);
	}

	@Override
	public long getItemId(int position) {
		// TODO Auto-generated method stub
		return position;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		View v = null;
		NoteItem note = null;
		TextView twTitle = null;
		TextView twTime = null;

		note = (NoteItem)getItem(position);
		if( mInflater == null || note == null )
			return null;
		
		//Android ListView has a bug here, if use the old one, the list will be reversed.
		if( convertView != null )
			v = convertView;
		else
			v = mInflater.inflate(R.layout.note_item, null);	
		
		twTitle = (TextView)v.findViewById(R.id.note_title);
		if( twTitle != null )
			twTitle.setText( note.Title );
		
		twTime = (TextView)v.findViewById(R.id.note_time);			
		if( twTime != null )
			twTime.setText( new Date(note.ID).toLocaleString() );
		
		return v;
	}

}
